package com.bjpowernode.dataservice.service.impl;

import com.bjpowernode.api.model.RechargeRecord;
import com.bjpowernode.api.service.RechargeService;
import com.bjpowernode.api.vo.RechargeInfo;
import com.bjpowernode.contacts.YLBConst;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.RechargeRecordMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * ClassName:RechargeServiceImpl
 * Date:2021/12/28 11:59
 */
@DubboService(interfaceClass = RechargeService.class,version = "1.0")
public class RechargeServiceImpl implements RechargeService {

    @Resource
    private RechargeRecordMapper rechargeMapper;

    @Resource
    private FinanceAccountMapper financeAccountMapper;

    /*某个用户的充值记录列表，支持分页*/
    @Override
    public List<RechargeInfo> queryUserRechargeByUserId(Integer userId, Integer pageNo, Integer pageSize) {
        if( pageNo == null || pageNo < 1 ){
            pageNo = 1;
        }
        if(pageSize == null || pageSize < 0) {
            pageSize = 5;
        }
        int offset = (pageNo -1 ) * pageSize;
        List<RechargeInfo> infoList =  rechargeMapper.selectByUserId(userId,offset,pageSize);
        return infoList;
    }


    /*创建充值记录*/
    @Override
    public int addRecharge(RechargeRecord record) {
        int rows = 0;
        if( record != null){
            rows = rechargeMapper.insertSelective(record);
        }
        return rows;

    }

    /*处理充值*/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized int handlerRecharge(String kq, String orderId, String payResult, String payAmount) {

        int result = -1;
        int rows = 0;
        //1.判断订单orderId是否是商家的
        RechargeRecord record = rechargeMapper.selectByOrderId(orderId);
        if( record != null){
            //2判断状态
            if( record.getRechargeStatus() == YLBConst.RECHARGE_PROCESSING){
                //3.判断金额是否一致
                String fen = record.getRechargeMoney().multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();
                if( fen.equals(payAmount)){
                    //4.可以给用户充值，判断在快钱方，充值是否成功
                    if("10".equals(payResult)){
                        //成功 给资金账号增加金额， 修改充值记录为1
                        rows = financeAccountMapper.updateByRecharge(record.getUid(),record.getRechargeMoney());
                        if(rows <1){
                            throw new RuntimeException("充值异步通知，更新账号资金失败");
                        }
                        rows = rechargeMapper.updateStatus(record.getId(),YLBConst.RECHARGE_SUCCESS);
                        if( rows <1 ){
                            throw new RuntimeException("充值异步通知，更新充值记录状态为1时失败");
                        }
                    } else {
                        //失败
                        rows = rechargeMapper.updateStatus(record.getId(),YLBConst.RECHARGE_FAIL);
                        if( rows <1 ){
                            throw new RuntimeException("充值异步通知，更新充值记录状态为2时失败");
                        }
                    }
                    result = 0;//成功

                } else {
                    result = 1;//金额不一致
                }
            } else {
                result = 2; //订单已经处理过了
            }
        } else {
            result = 3;//订单不存在
        }
        return result;
    }
}
